package com.thought.wxsession.utils;

import com.tencent.wework.Finance;

import java.io.File;
import java.io.FileOutputStream;

/**
 * @Author：zhuqinyue
 * @Package：com.utils
 * @Project：wechat-serve
 * @name：SessionUtils
 * @Date：2022/12/20 16:16
 * @Filename：SessionUtils
 */
public class SessionUtils {



    public String downMedia(String sdkField,String media_path,Long sdk) {

        String indexbuf = "";
        while (true) {
            long media_data = Finance.NewMediaData();
            int ret = Finance.GetMediaData(sdk, indexbuf, sdkField, "", "", 60, media_data);
            if (ret != 0) {
                return null;
            }
            try {
                FileOutputStream outputStream = new FileOutputStream(media_path, true);
                outputStream.write(Finance.GetData(media_data));
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (Finance.IsMediaDataFinish(media_data) == 1) {
                Finance.FreeMediaData(media_data);
                break;
            } else {
                indexbuf = Finance.GetOutIndexBuf(media_data);
                Finance.FreeMediaData(media_data);
            }
        }
        String url = media_path;
        try {
            url = media_path;
            url = CosUtils.uploadFile(new File(media_path), media_path);
            return url;
        }catch (Exception e){
            e.printStackTrace();
        }
            return null;
    }
}
